/*
 * Created by JFormDesigner on Sun Jul 31 08:33:46 EDT 2011
 */

package ca.etsmtl.log730.projet2.view;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.UUID;

import javax.swing.GroupLayout;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.LayoutStyle;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.beansbinding.Binding;
import org.jdesktop.beansbinding.BindingGroup;
import org.jdesktop.beansbinding.Bindings;
import org.jdesktop.beansbinding.ELProperty;
import org.jdesktop.swingx.JXButton;
import org.jdesktop.swingx.JXFrame;
import org.jdesktop.swingx.JXLabel;
import org.jdesktop.swingx.JXPanel;
import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.JXTextField;

import ca.etsmtl.log730.projet2.connectors.clientserver.LinkHandler;
import ca.etsmtl.log730.projet2.connectors.clientserver.setup.ConnectionData;
import ca.etsmtl.log730.projet2.connectors.clientserver.setup.LocalServerData;
import ca.etsmtl.log730.projet2.documents.Document;
import ca.etsmtl.log730.projet2.users.User;
import ca.etsmtl.log730.projet2.view.data.DocumentTableModel;
import ca.etsmtl.log730.projet2.view.data.SessionTableModel;

/**
 * @author Joe Labrosse
 */
public class JXMainFrame extends JXFrame {
	public JXMainFrame() {
		initComponents();
	}

	public LinkHandler getLinkHandler() {
		return linkHandler;
	}

	private void btOpenActionPerformed(ActionEvent e) {
		// TODO add your code here
		if(this.tableFiles.getSelectedRow() != -1) {
			final UUID id = (UUID)documentTableModel.getValueAt(tableFiles.getSelectedRow(),0);
//			Request r = linkHandler.getRequestManager().openView(id);
			SwingUtilities.invokeLater(new Runnable() {

				@Override
				public void run() {
					JXDocumentEdit jc = new JXDocumentEdit();
					jc.setLinkHandler(linkHandler);
					linkHandler.getRequestManager().openView(id);
					jc.setVisible(true);
//					linkHandler.getRequestManager().register(jc, linkHandler.getRequestManager().openView(id));
				}
			});
				
		}
	}

	private void btAddDocActionPerformed(ActionEvent e) {
		linkHandler.getRequestManager().insertDocument(UUID.randomUUID(), new Document("NOUVEAU.txt"));
		// TODO add your code here
	}

	private void btConnectActionPerformed(ActionEvent e) {
		// TODO add your code here
		linkHandler.connect(connectionData);
	}

	private void btStartButtonActionPerformed(ActionEvent e) {
		// TODO add your code here
		SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {
				bindConnection.unbind();
				linkHandler.getNodesMap().getCurrentNode().setUser(user);
				linkHandler.getNodesMap().getCurrentNode().setConnectionData(localServerData);
				linkHandler.start();
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				bindConnection.bind();
			}
			
		});
	}

	private void createUIComponents() {
		// TODO: add custom component creation code here
	}

	public LocalServerData getLocalServerData() {
		return localServerData;
	}

	public User getUser() {
		return user;
	}

	private void btCloseActionPerformed(ActionEvent e) {
		// TODO add your code here
		System.exit(0);
	}

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		// Generated using JFormDesigner Evaluation license - Joe Labrosse
		linkHandler = new LinkHandler();
		sessionTableModel = new SessionTableModel();
		documentTableModel = new DocumentTableModel();
		user = new User();
		connectionData = new ConnectionData();
		localServerData = new LocalServerData();
		localServerData = LocalServerData.getLocalServerMain();
		tabbedPane1 = new JTabbedPane();
		statusPanel = new JXPanel();
		xLabel1 = new JXLabel();
		xTextField1 = new JXTextField();
		xLabel2 = new JXLabel();
		xTextField2 = new JXTextField();
		xTextField3 = new JXTextField();
		btConnect = new JXButton();
		scrollPane1 = new JScrollPane();
		xTable1 = new JXTable();
		btStartButton = new JXButton();
		xTextField4 = new JXTextField();
		xLabel3 = new JXLabel();
		xTextField5 = new JXTextField();
		filesPanel = new JXPanel();
		scrollPane2 = new JScrollPane();
		tableFiles = new JXTable();
		btAddDoc = new JXButton();
		btDelDoc = new JXButton();
		btOpen = new JXButton();
		btClose = new JXButton();

		//======== this ========
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		setTitle("Super TextEditor 3000");
		Container contentPane = getContentPane();

		//======== tabbedPane1 ========
		{

			//======== statusPanel ========
			{

				// JFormDesigner evaluation mark


				//---- xLabel1 ----
				xLabel1.setText("Listening to:");

				//---- xTextField1 ----
				xTextField1.setEnabled(false);

				//---- xLabel2 ----
				xLabel2.setText("Connect to:");

				//---- btConnect ----
				btConnect.setText("Connect!");
				btConnect.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						btConnectActionPerformed(e);
					}
				});

				//======== scrollPane1 ========
				{
					scrollPane1.setViewportView(xTable1);
				}

				//---- btStartButton ----
				btStartButton.setText("Start!");
				btStartButton.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						btStartButtonActionPerformed(e);
					}
				});

				//---- xLabel3 ----
				xLabel3.setText("Username:");

				GroupLayout statusPanelLayout = new GroupLayout(statusPanel);
				statusPanel.setLayout(statusPanelLayout);
				statusPanelLayout.setHorizontalGroup(
					statusPanelLayout.createParallelGroup()
						.addGroup(statusPanelLayout.createSequentialGroup()
							.addContainerGap()
							.addGroup(statusPanelLayout.createParallelGroup()
								.addComponent(scrollPane1, GroupLayout.DEFAULT_SIZE, 625, Short.MAX_VALUE)
								.addGroup(statusPanelLayout.createSequentialGroup()
									.addGroup(statusPanelLayout.createParallelGroup()
										.addComponent(xLabel2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
										.addComponent(xLabel1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
										.addComponent(xLabel3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
									.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
									.addGroup(statusPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
										.addGroup(statusPanelLayout.createSequentialGroup()
											.addGroup(statusPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
												.addComponent(xTextField1, GroupLayout.PREFERRED_SIZE, 67, GroupLayout.PREFERRED_SIZE)
												.addComponent(xTextField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
											.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
											.addGroup(statusPanelLayout.createParallelGroup()
												.addComponent(xTextField3, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE)
												.addComponent(xTextField4, GroupLayout.PREFERRED_SIZE, 67, GroupLayout.PREFERRED_SIZE)))
										.addComponent(xTextField5, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
									.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
									.addGroup(statusPanelLayout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
										.addComponent(btStartButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
										.addComponent(btConnect, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))
							.addContainerGap())
				);
				statusPanelLayout.setVerticalGroup(
					statusPanelLayout.createParallelGroup()
						.addGroup(GroupLayout.Alignment.TRAILING, statusPanelLayout.createSequentialGroup()
							.addContainerGap()
							.addGroup(statusPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
								.addComponent(xLabel3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(xTextField5, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
							.addGroup(statusPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
								.addComponent(xLabel1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(btStartButton, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(xTextField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(xTextField4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
							.addGroup(statusPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
								.addComponent(xLabel2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(xTextField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(xTextField3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(btConnect, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
							.addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 321, GroupLayout.PREFERRED_SIZE)
							.addContainerGap())
				);
			}
			tabbedPane1.addTab("Status", statusPanel);


			//======== filesPanel ========
			{

				//======== scrollPane2 ========
				{
					scrollPane2.setViewportView(tableFiles);
				}

				//---- btAddDoc ----
				btAddDoc.setText("+");
				btAddDoc.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						btAddDocActionPerformed(e);
					}
				});

				//---- btDelDoc ----
				btDelDoc.setText("-");

				//---- btOpen ----
				btOpen.setText("View...");
				btOpen.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						btOpenActionPerformed(e);
					}
				});

				GroupLayout filesPanelLayout = new GroupLayout(filesPanel);
				filesPanel.setLayout(filesPanelLayout);
				filesPanelLayout.setHorizontalGroup(
					filesPanelLayout.createParallelGroup()
						.addGroup(filesPanelLayout.createSequentialGroup()
							.addContainerGap()
							.addGroup(filesPanelLayout.createParallelGroup()
								.addComponent(scrollPane2, GroupLayout.DEFAULT_SIZE, 625, Short.MAX_VALUE)
								.addGroup(filesPanelLayout.createSequentialGroup()
									.addComponent(btAddDoc, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
									.addComponent(btDelDoc, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 468, Short.MAX_VALUE)
									.addComponent(btOpen, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
							.addContainerGap())
				);
				filesPanelLayout.setVerticalGroup(
					filesPanelLayout.createParallelGroup()
						.addGroup(filesPanelLayout.createSequentialGroup()
							.addContainerGap()
							.addComponent(scrollPane2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
							.addGroup(filesPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
								.addComponent(btAddDoc, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(btDelDoc, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(btOpen, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addContainerGap(24, Short.MAX_VALUE))
				);
			}
			tabbedPane1.addTab("Files", filesPanel);

		}

		//---- btClose ----
		btClose.setText("Quit");
		btClose.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				btCloseActionPerformed(e);
			}
		});

		GroupLayout contentPaneLayout = new GroupLayout(contentPane);
		contentPane.setLayout(contentPaneLayout);
		contentPaneLayout.setHorizontalGroup(
			contentPaneLayout.createParallelGroup()
				.addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
						.addComponent(btClose, GroupLayout.PREFERRED_SIZE, 101, GroupLayout.PREFERRED_SIZE)
						.addComponent(tabbedPane1, GroupLayout.DEFAULT_SIZE, 654, Short.MAX_VALUE))
					.addContainerGap())
		);
		contentPaneLayout.setVerticalGroup(
			contentPaneLayout.createParallelGroup()
				.addGroup(contentPaneLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(tabbedPane1, GroupLayout.PREFERRED_SIZE, 472, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addComponent(btClose, GroupLayout.DEFAULT_SIZE, 28, Short.MAX_VALUE)
					.addContainerGap())
		);
		pack();
		setLocationRelativeTo(getOwner());

		initComponentBindings();
		// JFormDesigner - End of component initialization  //GEN-END:initComponents
	}

	private void initComponentBindings() {
		// JFormDesigner - Component bindings initialization - DO NOT MODIFY  //GEN-BEGIN:initBindings
		// Generated using JFormDesigner Evaluation license - Joe Labrosse
		bindingGroup = new BindingGroup();
		bindingGroup.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ_WRITE,
			linkHandler, BeanProperty.create("nodesMap"),
			sessionTableModel, BeanProperty.create("sessionNodeMap")));
		bindingGroup.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ_WRITE,
			sessionTableModel, xTable1, BeanProperty.create("model")));
		bindingGroup.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ_WRITE,
			documentTableModel, tableFiles, BeanProperty.create("model")));
		bindingGroup.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ_WRITE,
			linkHandler, BeanProperty.create("requestManager.documentMap"),
			documentTableModel, BeanProperty.create("documentMap")));
		bindingGroup.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ_WRITE,
			connectionData, BeanProperty.create("hostname"),
			xTextField2, BeanProperty.create("text")));
		bindingGroup.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ_WRITE,
			connectionData, BeanProperty.create("port"),
			xTextField3, BeanProperty.create("text")));
		bindingGroup.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ_WRITE,
			localServerData, BeanProperty.create("hostname"),
			xTextField1, BeanProperty.create("text")));
		{
			Binding binding = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE,
				localServerData, BeanProperty.create("port"),
				xTextField4, BeanProperty.create("text"));
			bindingGroup.addBinding(binding);
			binding.bind();
		}
		bindingGroup.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ_WRITE,
			user, ELProperty.create("${name}"),
			xTextField5, BeanProperty.create("text")));
		bindingGroup.bind();
		bindConnection = new BindingGroup();
		bindConnection.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ,
			linkHandler, ELProperty.create("${!listening}"),
			btStartButton, BeanProperty.create("enabled")));
		bindConnection.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ,
			linkHandler, ELProperty.create("${listening}"),
			btConnect, BeanProperty.create("enabled")));
		bindConnection.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ_WRITE,
			linkHandler, ELProperty.create("${!listening}"),
			xTextField5, BeanProperty.create("editable")));
		bindConnection.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ_WRITE,
			linkHandler, ELProperty.create("${listening}"),
			xTextField2, BeanProperty.create("editable")));
		bindConnection.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ_WRITE,
			linkHandler, ELProperty.create("${listening}"),
			xTextField3, BeanProperty.create("editable")));
		bindConnection.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ_WRITE,
			linkHandler, ELProperty.create("${!listening}"),
			xTextField4, BeanProperty.create("editable")));
		bindConnection.bind();
		// JFormDesigner - End of component bindings initialization  //GEN-END:initBindings
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	// Generated using JFormDesigner Evaluation license - Joe Labrosse
	private LinkHandler linkHandler;
	private SessionTableModel sessionTableModel;
	private DocumentTableModel documentTableModel;
	private User user;
	private ConnectionData connectionData;
	public LocalServerData localServerData;
	private JTabbedPane tabbedPane1;
	private JXPanel statusPanel;
	private JXLabel xLabel1;
	private JXTextField xTextField1;
	private JXLabel xLabel2;
	private JXTextField xTextField2;
	private JXTextField xTextField3;
	private JXButton btConnect;
	private JScrollPane scrollPane1;
	private JXTable xTable1;
	private JXButton btStartButton;
	private JXTextField xTextField4;
	private JXLabel xLabel3;
	private JXTextField xTextField5;
	private JXPanel filesPanel;
	private JScrollPane scrollPane2;
	private JXTable tableFiles;
	private JXButton btAddDoc;
	private JXButton btDelDoc;
	private JXButton btOpen;
	private JXButton btClose;
	private BindingGroup bindingGroup;
	private BindingGroup bindConnection;
	// JFormDesigner - End of variables declaration  //GEN-END:variables
}
